/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 */

package com.device.cp.penspeeddemo;

import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * JNI PenSpeedProxy接口
 *
 * @since 2021-07-28
 */
public class PenSpeedProxy {
    private static final String TAG = "PenSpeedProxy";
    private static final String SERVICE_NAME = "wb_fastpen";

    static {
        try {
            System.loadLibrary("native-lib");
        } catch (UnsatisfiedLinkError e) {
            Log.e(TAG, "native-lib ShareLibrary load failed.", e);
        }
    }

    private MainActivity mActivity;

    /**
     * fastpen是否ok
     */
    public boolean isFastPenOk = false;

    /**
     * PenSpeed native 库的代理类
     *
     * @param mainActivity 画布窗口对象传递保存
     */
    public PenSpeedProxy(MainActivity mainActivity) {
        mActivity = mainActivity;
        if (Build.VERSION.SDK_INT > 28) {
            try {
                Class<?> smClass = Class.forName("com.huawei.android.os.ServiceManagerEx");
                Method getFun = smClass.getDeclaredMethod("getService", String.class);
                Object obj = getFun.invoke(null, SERVICE_NAME); // wb_fastpen
                IBinder serverBinder = null;
                if (obj instanceof IBinder) {
                    serverBinder = (IBinder) obj;
                }
                if (serverBinder != null) {
                    Log.i(TAG, "get serverBinder ok");
                    isFastPenOk = (createNativePenSpeedInterface(serverBinder) == 0);
                } else {
                    Log.e(TAG, "getService " + SERVICE_NAME + " fail!!!");
                }
            } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException
                    | InvocationTargetException e) {
                Log.e(TAG, SERVICE_NAME + " not found!!! catch exception " + e.toString());
            }
        } else {
            if (createNativePenSpeedInterface(null) == 0) {
                isFastPenOk = true;
            }
        }
    }

    /**
     * 创建画布
     *
     * @param left 屏幕坐标Canvas左边界
     * @param top 屏幕坐标Canvas上边界
     * @param right 屏幕坐标Canvas右边界
     * @param bottom 屏幕坐标Canvas下边界
     */
    public void fbCreateCanvas(int left, int top, int right, int bottom) {
        this.nativeFbCreateCanvas(left, top, right - left, bottom - top);
    }

    /**
     * 删除画布
     */
    public void fbDeleteCanvas() {
        this.nativeFbDeleteCanvas();
    }

    /**
     * 处理落笔事件
     *
     * @param pointerId 点id
     * @param pointX 屏幕x坐标
     * @param pointY 屏幕y坐标
     */
    public void onDown(int pointerId, float pointX, float pointY) {
        this.nativeFbStart(pointerId, pointX, pointY);
    }

    /**
     * 处理移动事件
     *
     * @param pointerId 点id
     */
    public void onMove(int pointerId) {
        this.nativeFbGetPt(pointerId);
    }

    /**
     * 处理抬笔事件
     *
     * @param pointerId 点id
     */
    public void onUp(int pointerId) {
        this.nativeFbStop(pointerId);
    }

    /**
     * 释放native资源
     */
    public void release() {
        nativeDelete();
        mActivity = null;
    }

    /**
     * fastpen java回调函数处理函数
     *
     * @param pointId 触点Id
     * @param posXInView 相对于view坐标系下的x轴坐标
     * @param posYInView 相对于view坐标系下的y轴坐标
     */
    public void fastPenOnTouch(int pointId, float posXInView, float posYInView) {
        if (mActivity != null) {
            mActivity.fastPenOnTouch(pointId, posXInView, posYInView);
        }
    }

    /**
     * 创建Native的PenSpeedInterface对象并初始化
     *
     * @param srvBinder fastpen的IBinder对象
     * @return 0 代表成功， 1 代表失败
     */
    public native int createNativePenSpeedInterface(Object srvBinder);

    /**
     * 指定参数新建画布
     *
     * @param posX 屏幕坐标系下左上角点的x轴数值
     * @param posY 屏幕坐标系下左上角点的y轴数值
     * @param width 屏幕坐标系画布宽度
     * @param height 屏幕坐标系画布高度
     */
    public native void nativeFbCreateCanvas(int posX, int posY, int width, int height);

    /**
     * 删除画布
     */
    public native void nativeFbDeleteCanvas();

    /**
     * 开始书写
     *
     * @param pointerId 触点Id
     * @param pointX 屏幕x坐标
     * @param pointY 屏幕y坐标
     */
    public native void nativeFbStart(int pointerId, float pointX, float pointY);

    /**
     * 主动获取书写触点的点并处理
     *
     * @param pointerId 触点Id
     */
    public native void nativeFbGetPt(int pointerId);

    /**
     * 停止书写
     *
     * @param pointerId 触点Id
     */
    public native void nativeFbStop(int pointerId);

    /**
     * 释放native资源
     */
    public native void nativeDelete();

    /**
     * 设置fastpen笔的颜色和宽度
     *
     * @param argbColor fastpen笔的颜色
     * @param penWidth fastpen笔的宽度
     */
    public native void nativeSetFastPenColorWidth(int argbColor, float penWidth);

    /**
     * 设置取点方式
     *
     * @param isUseGetWay true 使用主动获取点的方式, false 使用回调处理
     */
    public native void nativeSetGetPointerWay(boolean isUseGetWay);
}
